<?php

	class Ad extends CI_Model
	{
		public function __construct()
		{
			parent::__construct();
		}

		function getAd($id = false)
		{
			$query = $this->db->get_where('ads',array('id' => $id));
			return $query->row_array();
		}

		function updateAd($data,$id)
		{
			$this->db->where('id',$id);
			return $this->db->update('ads',$data);
		}
		
		function getAllAds()
		{
			$this->db->order_by('order','asc');
			$query = $this->db->get('ads');			
			return $query->result_array();
		}

		function getLeftAds()
		{
			$this->db->select('*');
			$this->db->from('ads');
			$this->db->join('positions','positions.id = ads.position_id');
			$this->db->where(array('positions.name' => 'left'));

			$query = $this->db->get();
			return $query->result_array();
		}

		function getRightAds()
		{
			$this->db->select('*');
			$this->db->from('ads');
			$this->db->join('positions','positions.id = ads.position_id');
			$this->db->where(array('positions.name' => 'right'));

			$query = $this->db->get();
			return $query->result_array();
		}

		function getHomeAds()
		{
			$this->db->select('*');
			$this->db->from('ads');
			$this->db->join('positions','positions.id = ads.position_id');
			$this->db->where(array('positions.name' => 'home'));

			$query = $this->db->get();
			return $query->result_array();
		}

		function addNewAd($data)
		{
			return $this->db->insert('ads',$data);
		}		
	}